package com.justgame.project.base.config;

import com.justgame.project.base.support.SysUserInfoArgumentResolver;
import com.justgame.project.base.interceptor.FeignApiProtectInterceptor;
import com.justgame.project.base.interceptor.HttpRequestInterceptor;
import com.justgame.project.base.interceptor.ProtectAccessInterceptor;
import com.justgame.project.base.interceptor.VerificationCodeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * 拦截器配置
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Resource
    private VerificationCodeInterceptor verificationCodeInterceptor;

    @Resource
    private ProtectAccessInterceptor protectAccessInterceptor;

    @Resource
    private HttpRequestInterceptor httpRequestInterceptor;

    @Resource
    private FeignApiProtectInterceptor feignApiProtectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /* 添加Controller拦截器 */
        // 1.请求拦截器，操作Request
        registry.addInterceptor(httpRequestInterceptor).addPathPatterns("/**");
        // 2.验证码校验
        registry.addInterceptor(verificationCodeInterceptor).addPathPatterns("/**");
        // 3.访问保护
        registry.addInterceptor(protectAccessInterceptor).addPathPatterns("/**");
        // 4.Feign内部接保护
        registry.addInterceptor(feignApiProtectInterceptor).addPathPatterns("/feign/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(0, new SysUserInfoArgumentResolver());
    }
}
